<template>
  <div class="addtemplate">
    <el-dialog :title="title" :visible.sync="dialogFormTempVisible" center>
      <el-form :model="formData" :rules="rules" ref="templateFrom" label-width="80px">
        <el-form-item label="选择产品">
          <el-select 
            v-model="formData.appitemId" 
            placeholder="请选择产品"
            v-selectLoadMore="selectLoadMore">
            <el-option v-for="item in optionList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板名称">
          <el-input v-model="formData.name" autocomplete="off" maxlength="15" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="formData.note" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormTempVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureAddTemplate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addTemplate, getAppList } from "@/api/basisMG";
export default {
  props: {
    addtempDialogVisible: {
      type: Boolean,
      default: false,
    },
    appType: {
      type: String,
    },
  },
  data() {
    return {
      dialogFormTempVisible: false,
      title: "",
      formData: {
        id: "", //模板id
        appitemId: "", //模板产品id
        name: "",
        note: "",
      },
      rules: {
        name: [{ required: true, message: "请填写类型名称", trigger: "blur" }],
      },
      optionList: [],
      formAppData: {
        type: "",
        name: "",
        pageSize: 10,
        pageNum: 1,
      },
      total: 0
    };
  },
  methods: {
    // 下拉加载更多
    selectLoadMore () {
      this.formAppData.pageSize = this.formAppData.pageSize + 1;
      if (this.formAppData.pageSize > this.total) return
      this.getAppList()
    },
    // 获取产品列表
    async getAppList() {
      this.formAppData.type = this.appType;
      const res = await getAppList(this.formAppData);
      if (res.code === '200') {
        this.optionList = res.data.content;
        this.total = res.data.total;
      } else {
        this.optionList = [];
      }
    },
    showEdit(row) {
      this.formAppData.pageSize = 10;
      this.getAppList();
      if (row.isSet == 0) {
        this.dialogFormTempVisible = true;
        this.title = "添加模板";
        this.formData.name = "";
        this.formData.note = "";
        this.formData.appitemId = "";
        this.formData.id = "";
      } else {
        this.dialogFormTempVisible = true;
        this.title = "编辑模板";
        this.formData.name = row.name;
        this.formData.note = row.note;
        this.formData.appitemId = row.appitemId;
        this.formData.id = row.id;
      }
    },
    sureAddTemplate() {
      this.$refs["templateFrom"].validate(async (valid) => {
        if (valid) {
          const data = await addTemplate(this.formData);
          if (data.code != 200) return this.$message.error(data.message);
          this.$message.success(data.message);
          setTimeout(() => {
            this.dialogFormTempVisible = false;
            this.emitList();
          }, 500);
        } else {
          this.$message.warning("提交失败, 请检查内容是否填写正确!!!");
          return false;
        }
      });
    },
    emitList(e) {
      this.$emit("editorList", e);
    },
  },
  created() {},
  watch: {
    // 监听 addtempDialogVisible 改变
    addtempDialogVisible(oldVal, newVal) {
      this.dialogFormTempVisible = this.addtempDialogVisible;
    },
  },
};
</script>
<style lang="scss"></style>
